package cn.edu.gdu.quizpractice.controller.mini;

import cn.edu.gdu.quizpractice.entity.ApiResult;
import cn.edu.gdu.quizpractice.entity.ExamTime;
import cn.edu.gdu.quizpractice.service.ICategoryService;
import cn.edu.gdu.quizpractice.service.IEmailService;
import cn.edu.gdu.quizpractice.service.IExamTimeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: Aubery
 * @Date: 2025-04-17-0:21
 * @Description:
 */
@RestController
@RequestMapping({"/mini", "/web/mini"}) // 同时映射两个路径
@Slf4j
public class OtherController {

    @Autowired
    private IExamTimeService examTimeService;

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/setExamTime")
    public ApiResult setExamTime(ExamTime examTime){
        int i = examTimeService.insertExamTime(examTime);
        if(i > 0)
            return ApiResult.success();
        else
            return ApiResult.fail("设置考试时间失败");
    }

    @RequestMapping("/getExamTime")
    public ApiResult getExamTime(){
        return ApiResult.success(examTimeService.findExamTimeByYear(LocalDate.now().getYear()));
    }


    @RequestMapping("/getDays")
    public ApiResult getDays() throws DateTimeException {
        // 获取考试时间
        ExamTime examTime = examTimeService.findExamTimeByYear(LocalDate.now().getYear());
        if (examTime == null || examTime.getExamDate() == null) {
            return ApiResult.fail("未设置考试时间");
        }

        LocalDate examDate = examTime.getExamDate();
        LocalDate currentDate = LocalDate.now();

        // 计算剩余天数（支持跨年、正确处理负数）
        long daysRemaining = ChronoUnit.DAYS.between(currentDate, examDate);

        // 如果考试已过，可以返回0或负数
        return ApiResult.success(daysRemaining);
    }

    @RequestMapping("/setSlideshow")
    private ApiResult setSlideshow(){
        List<String> slideshow = new ArrayList<>();
        slideshow.set(0, "https://www.baidu.com");
        slideshow.set(1, "https://www.baidu.com");
        slideshow.set(2, "https://www.baidu.com");
        slideshow.set(3, "https://www.baidu.com");
        return ApiResult.success();
    }

    @RequestMapping("/getSlideshow")
    public ApiResult getSlideshow(){
        log.info("getSlideshow");
        List<String> slideshow = new ArrayList<>();
        slideshow.add("https://gitee.com/auberry/Pic/raw/master/kyti.png");
        slideshow.add("https://gitee.com/auberry/Pic/raw/master/kytkmf.png");
        return ApiResult.success(slideshow);
    }

    @RequestMapping("/getCategories")
    public ApiResult getCategories(){
        return ApiResult.success(categoryService.findAll());
    }

    @RequestMapping("/getBanksByCategory")
    public ApiResult getBanksByCategory(@RequestParam("categoryName") String categoryName){
        return ApiResult.success(categoryService.findBanksByCategoryName(categoryName));
    }

    @RequestMapping("/getAllExamTime")
    public ApiResult getAllExamTime(){
        return ApiResult.success(examTimeService.findAllExamTime());
    }

}
